/*
 * @Author: huangwei
 * @Date: 2021-06-09 17:57:42
 * @LastEditors: 黄威
 * @LastEditTime: 2021-10-13 17:50:32
 * @Description: leaflet draw
 */
import { DRAGHOOKS, AbsDraw, ISimpleHandler } from './AbsDraw'
import newGraphic from '../bind-map/graphic'

// 事件映射
const EventMapper = {
    lclick: 'click',
    rclick: 'contextmenu',
    move: 'mousemove',
    ldown: 'mousedown',
    lup: 'mouseup',
}

// leaflet draw 对象
export default class LDraw extends AbsDraw {
    _on(type, event) {
        this.map.on(EventMapper[type], event)
    }

    _off(type, event) {
        this.map.off(EventMapper[type], event)
    }

    _evtToCoord(evt) {
        const { lat, lng } = evt.latlng
        return [lng, lat]
    }

    _setDragAble(flag) {
        flag ? this.map.dragging.enable() : this.map.dragging.disable()
    }
}

/* ********************  目前支持的一些方法 ********************** */
// 点
class LPointHandler extends ISimpleHandler {
    MaxLen = 1

    _newGraphic() {
        return newGraphic('Point')
    }
}

// 图标
class LMarkerHandler extends LPointHandler {
    _newGraphic() {
        return newGraphic('Marker')
    }
}

// 线
class LPolylineHandler extends ISimpleHandler {
    MinLen = 2

    get _guideStyle() {
        return {
            ...this.style,
            dashArray: [8, 16],
            fillOpacity: 0.1,
            interactive: false,
        }
    }

    _newGraphic() {
        return newGraphic('Polyline')
    }

    _newGuide() {
        return this._newGraphic().setStyle(this._guideStyle).addTo(this.target)
    }
    _updateGuide(coord) {
        if (this.coords.length) {
            this._guideG.setCoords([this.coords[this.coords.length - 1], coord])
        }
    }
}

// 面
class LPolygonHandler extends LPolylineHandler {
    MinLen = 3

    _newGraphic() {
        return newGraphic('Polygon')
    }

    _updateGuide(coord) {
        if (this.coords.length) {
            const coords = []

            coords.push(this.coords[this.coords.length - 1])
            coords.push(coord)

            if (this.coords.length >= 2) {
                coords.push(this.coords[0])
            }

            this._guideG.setCoords(coords)
        }
    }
}

class LDragHandler extends LPolygonHandler {
    MinLen = 2
    MaxLen = 2

    hooks = {
        ...DRAGHOOKS,
        ldown: function (drawIns, coord, evt) {
            if (evt && evt.originalEvent.button !== 0) return
            DRAGHOOKS.ldown(drawIns, coord)
        },
        lup: function (drawIns, coord, evt) {
            if (evt && evt.originalEvent.button !== 0) return
            DRAGHOOKS.lup(drawIns, coord)
        },
    }
}

// 矩形
class LRectangleHandler extends LDragHandler {
    _newGraphic() {
        return newGraphic('Rectangle')
    }

    _updateGuide(coord) {
        if (this.coords.length) {
            this._guideG.setCoords([this.coords[0], coord])
        }
    }
}

// 圆
class LCircleHandler extends LDragHandler {
    _newGraphic() {
        return newGraphic('Circle')
    }

    _newGuide() {
        return this._newGraphic().setStyle(this._guideStyle)
    }
    _updateGuide(coord) {
        if (this.coords.length) {
            this._guideG.setCoords([this.coords[0], coord])

            // 未添加
            if (!this._guideG._mapToAdd) {
                this._guideG.addTo(this._draw.map)
            }
        }
    }
}

LDraw.modes = {
    Point: LPointHandler,
    Marker: LMarkerHandler,
    Polyline: LPolylineHandler,
    Polygon: LPolygonHandler,
    Rectangle: LRectangleHandler,
    Circle: LCircleHandler,
}
